<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>工作流选择器 - LangChainGraph</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        .container {
            background: white;
            border-radius: 12px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            padding: 30px;
            width: 100%;
            max-width: 500px;
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
        }

        .header h1 {
            color: #333;
            font-size: 28px;
            font-weight: 600;
            margin-bottom: 10px;
        }

        .header p {
            color: #666;
            font-size: 16px;
        }

        .workflow-selector {
            margin-bottom: 25px;
        }

        .workflow-selector label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #333;
        }

        .workflow-selector select {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 16px;
            background: white;
            transition: border-color 0.3s ease;
        }

        .workflow-selector select:focus {
            outline: none;
            border-color: #667eea;
        }

        .workflow-info {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 25px;
            border-left: 4px solid #667eea;
        }

        .workflow-info h3 {
            color: #333;
            margin-bottom: 10px;
            font-size: 18px;
        }

        .workflow-info p {
            color: #666;
            line-height: 1.6;
        }

        .buttons {
            display: flex;
            gap: 12px;
        }

        button {
            flex: 1;
            padding: 12px 20px;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .switch-btn {
            background: #667eea;
            color: white;
        }

        .switch-btn:hover {
            background: #5a6fd8;
            transform: translateY(-2px);
        }

        .refresh-btn {
            background: #f8f9fa;
            color: #333;
            border: 1px solid #e1e5e9;
        }

        .refresh-btn:hover {
            background: #e9ecef;
        }

        .status {
            margin-top: 20px;
            padding: 12px;
            border-radius: 6px;
            text-align: center;
            font-size: 14px;
        }

        .status.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .status.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .status.loading {
            background: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }

        .loading-spinner {
            display: inline-block;
            width: 16px;
            height: 16px;
            border: 2px solid #ffffff;
            border-radius: 50%;
            border-top-color: transparent;
            animation: spin 1s linear infinite;
            margin-right: 8px;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>工作流选择器</h1>
            <p>选择适合您需求的研究工作流</p>
        </div>

        <div class="workflow-selector">
            <label for="workflowSelect">选择工作流：</label>
            <select id="workflowSelect">
                <option value="">加载中...</option>
            </select>
        </div>

        <div class="workflow-info" id="workflowInfo">
            <h3>工作流信息</h3>
            <p>请选择一个工作流查看详细信息</p>
        </div>

        <div class="buttons">
            <button class="switch-btn" onclick="switchWorkflow()">
                切换工作流
            </button>
            <button class="refresh-btn" onclick="loadWorkflows()">
                刷新列表
            </button>
        </div>

        <div class="status" id="statusMessage" style="display: none;"></div>
    </div>

    <script>
        let workflows = [];

        // 页面加载时获取工作流列表
        document.addEventListener('DOMContentLoaded', loadWorkflows);

        async function loadWorkflows() {
            showStatus('正在加载工作流列表...', 'loading');
            
            try {
                const response = await fetch('/api/research/workflows');
                if (!response.ok) {
                    throw new Error(`HTTP错误: ${response.status}`);
                }
                
                workflows = await response.json();
                populateWorkflowSelect(workflows);
                updateCurrentWorkflowInfo();
                hideStatus();
            } catch (error) {
                showStatus(`加载失败: ${error.message}`, 'error');
                console.error('加载工作流失败:', error);
            }
        }

        function populateWorkflowSelect(workflows) {
            const select = document.getElementById('workflowSelect');
            select.innerHTML = '';
            
            workflows.forEach(workflow => {
                const option = document.createElement('option');
                option.value = workflow.name;
                option.textContent = workflow.display_name;
                select.appendChild(option);
            });
            
            // 设置当前选中的工作流
            updateSelectedWorkflow();
        }

        async function updateSelectedWorkflow() {
            try {
                const response = await fetch('/api/research/workflows/current');
                if (response.ok) {
                    const data = await response.json();
                    const select = document.getElementById('workflowSelect');
                    select.value = data.current_workflow;
                    updateWorkflowInfo(data.current_workflow);
                }
            } catch (error) {
                console.error('获取当前工作流失败:', error);
            }
        }

        async function updateCurrentWorkflowInfo() {
            try {
                const response = await fetch('/api/research/workflows/current');
                if (response.ok) {
                    const data = await response.json();
                    updateWorkflowInfo(data.current_workflow);
                }
            } catch (error) {
                console.error('获取当前工作流信息失败:', error);
            }
        }

        function updateWorkflowInfo(workflowName) {
            const workflow = workflows.find(w => w.name === workflowName);
            const infoDiv = document.getElementById('workflowInfo');
            
            if (workflow) {
                infoDiv.innerHTML = `
                    <h3>${workflow.display_name}</h3>
                    <p>${workflow.description}</p>
                    <p><strong>标识符:</strong> ${workflow.name}</p>
                `;
            } else {
                infoDiv.innerHTML = `
                    <h3>未知工作流</h3>
                    <p>无法找到工作流 "${workflowName}" 的详细信息</p>
                `;
            }
        }

        async function switchWorkflow() {
            const select = document.getElementById('workflowSelect');
            const workflowName = select.value;
            
            if (!workflowName) {
                showStatus('请先选择一个工作流', 'error');
                return;
            }
            
            showStatus('正在切换工作流...', 'loading');
            
            try {
                const response = await fetch('/api/research/workflows/switch', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ workflow_name: workflowName })
                });
                
                if (response.ok) {
                    const result = await response.json();
                    showStatus(result.message, 'success');
                    updateWorkflowInfo(workflowName);
                    
                    // 3秒后自动隐藏成功消息
                    setTimeout(hideStatus, 3000);
                } else {
                    const error = await response.json();
                    throw new Error(error.detail || '切换失败');
                }
            } catch (error) {
                showStatus(`切换失败: ${error.message}`, 'error');
                console.error('切换工作流失败:', error);
            }
        }

        function showStatus(message, type = 'info') {
            const statusDiv = document.getElementById('statusMessage');
            statusDiv.textContent = message;
            statusDiv.className = `status ${type}`;
            statusDiv.style.display = 'block';
        }

        function hideStatus() {
            const statusDiv = document.getElementById('statusMessage');
            statusDiv.style.display = 'none';
        }

        // 监听选择变化
        document.getElementById('workflowSelect').addEventListener('change', function() {
            const selectedWorkflow = this.value;
            if (selectedWorkflow) {
                updateWorkflowInfo(selectedWorkflow);
            }
        });
    </script>
</body>
</html>
